<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use Auth;
use Mail;
class UsersController extends Controller
{

  public function __construct()
 {
   $this->middleware('auth', [
      'except' => ['show', 'create', 'store','index','confirmEmail']
   ]);
   // 游客
   $this->middleware('guest', [
           'only' => ['create'],
       ]);
}


   public function index()
   {
       $users=User::paginate(8);
       return view('users.index', compact('users'));
   }

    //register
   public function create()
   {


     return view('users.create');
   }

   //展示个人信息
   public function show(User $user)
   {
     try{
      $statuses = $user->statuses()
                         ->orderBy('created_at', 'desc')
                         ->paginate(30);
      return view('users.show', compact('user', 'statuses'));
     } catch(\Exception $e){
        abort(404,$e->getMessage());
     }
   }
   //store
   public function store(Request $request)
   {
        $this->validate($request,[
          'name' => 'required|max:50',
          'email' => 'required|email|unique:users|max:255',
          'password' => 'required|confirmed|min:6'
        ]);
        //保存用户信息
         $user = User::create([
               'name' => $request->name,
               'email' => $request->email,
               'password' => bcrypt($request->password),
                ]);

        $this->sendEmailConfirmationTo($user);
        session()->flash('success', '验证邮件已发送到你的注册邮箱上，请注意查收。');
        return redirect('/');

   }

    //send E-Mail
    protected  function sendEmailConfirmationTo($user)
    {
       $view='emails.confirm';//email (view)
       $data = compact('user');//用户数据
       $from = "aufree@yousails.com";
       $name = "Aufree";
       $to = $user->email;
       $subject ="感谢注册 larawebo 应用！请确认 您的邮箱 ";
       Mail::send($view,$data, function($message) use ($from,$name,$to,$subject){
            $message->from($from,$name)->to($to)->subject($subject);
        });


    }

   //修改个人信息
   public function edit(User $user)
   {

     try {
          $this->authorize ('update', $user);
         return view ('users.edit', compact ('user'));
      } catch (\Exception $e) {
          abort(500, $e->getMessage());
      }

   }
   public function update(User $user,Request $request)
   {
          //验证
           $this->validate($request,[
            'name' => 'required|max:50',
            'password' => 'nullable|confirmed|min:6'
          ]);
          //验证权限
          $this->authorize('update',$user);
          $data=[];
          $data['name']=$request->name;
          if($request->password){

            $data['password']=bcrypt($request->password);
          }

          $user->update($data);
          session()->flash('success',"个人资料更新成功");

         return redirect()->route('users.show',$user->id);
    }
   //删除用户
    public function destroy(User $user)
    {
         $this->authorize('destroy',$user);//验证:只有管理员才可以删除
         $user->delete();
         session()->flash('success','成功删除用户');
        return  back();
    }
    //激活确认[邮箱]

   public function confirmEmail($token)
   {
     $user = User::where('activation_token', $token)->firstOrFail();

     $user->activated=true;
     $user->remember_token=str_random(10);
     $user->activation_token=$token;
     $user->save();
     //激活后直接登陆跳转
     Auth::login($user);
     session()->flash('success',"恭喜你激活larawebo成功");
     return redirect()->route('users.show',[$user]);

   }
  //followings
   public function followings(User $user)
   {
       $users=$user->followings()->paginate(25);
       $title="正在关注的人";
       return view('users.show_follow',compact('users','title'));

   }
   //followers
   public function followers(User $user)
   {
     $users= $user->followers()->paginate(25);
     $title="粉丝们";

    return view('users.show_follow',compact('users','title'));

   }







}
